﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NewAI.Game;
using NewAI.Tools;

namespace NewAI.FSM.TankFSM
{
    public class TankGetCoinsState:State<TankAI>
    {
        public override void Enter(TankAI entity)
        {
            entity.FightMethod = "采矿";
            //向目标金矿前进,进行无指令开火
            Direction? movDir = AstarDirect(entity);
            BulletType bulletType;
            if (movDir == null)
                movDir = entity.DefaultGetCoinDirection.Move;
            if (entity.DefaultGetCoinDirection.UseSuperBullet)
                bulletType = BulletType.Super;
            else
                bulletType = BulletType.Normal;
            PubliceMessage.Debug($"TankID = {entity.TankID} , 移动方向 = {movDir} ,子弹方向  = {entity.DefaultGetCoinDirection.Fire}");
            SendActionObj(entity, movDir, entity.DefaultGetCoinDirection.Fire,bulletType);
        }

        /// <summary>
        /// 调用AStar向金矿导航，获取移动提示
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        private Direction? AstarDirect(TankAI entity)
        {
            List<Point> path;
            Direction? dir;
            try
            {
                AStarPro.Astar(entity.Map, entity.MyTankInfo.Pos, entity.TargetCoinInfo.pos, out path);
                if(path.Count<=1)
                {
                    dir = null;
                    return dir;
                }
                else
                {
                    if (path[1].X - path[0].X > 0)
                    {
                        dir = Direction.Right;
                    }
                    else if (path[1].X - path[0].X < 0)
                        dir = Direction.Left;
                    else if (path[1].Y - path[0].Y > 0)
                        dir = Direction.Down;
                    else if (path[1].Y - path[0].Y < 0)
                        dir = Direction.Up;
                    else
                        dir = null;
                    return dir;
                }
               
            }
            catch (Exception)
            {
                dir = null;
                return dir;
            }

         
        }


        /// <summary>
        /// 发送移动指令
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="movDir"></param>
        private void SendActionObj(TankAI entity,Direction? movDir,Direction? firDir,BulletType bulletType)
        {
            ActionObj obj = new ActionObj(GV.OurTeam.TeamID, entity.MyTankInfo.TankID, movDir, firDir, bulletType);
            entity.ActionObjToJsonAnalyzer.Call(null, new FSMEventEventArg(obj));
        }
    }
}
